import { Injectable } from '@angular/core';
import { Http, Headers, Jsonp, URLSearchParams } from '@angular/http';
import { ServiceBase } from "./base";
import { MD5 } from "./../utils"

@Injectable()
export class Authentication extends ServiceBase {
    constructor(protected http: Http, protected jsonp: Jsonp) { super(); }
    /**
     * 
     * @param userName
     * @param password
     * @param sucb
     * @param errcb
     */
    async storeLogin(username: string, password: string) {
        let url = "/api/terminal/login/new";
        let postJson = {
            account: username,
            password: MD5.hex_md5(password)
        };
        const result = await this.Post(url, postJson);
        if (!result.user) {
            throw result;
        }
        this.StoreUser = result;
        this.Token = this.StoreUser.token;

        return this.StoreUser;
    }

    /**
     * 
     * @param userName
     * @param password
     * @param storeId
     */
    async employeeLogin(userName: string, password: string) {
        let url = "/api/auth/login";
        let postJson = {
            account: userName,
            password: MD5.hex_md5(password),
            storeId: this.CurrentStore.id
        };

        let result = await this.Post(url, postJson);
        this.Token = result.token;
        this.CurrentUser = {
            id: result.employee.userId,
            name: result.employee.employeeNumber
        };

        return result;
    }

    async getLDCConfig(storeId: string) {
        let url = "/api/stores/" + storeId + "/config/get/config?type=system.ldc";
        let result: any = await this.GetCloud(url);
        let ldcconfig = result.value;
        if (ldcconfig && ldcconfig.host) {
            this.LDCConfig = ldcconfig;
        }
        return this.LDCConfig;
    }

    employeeLogout() { }
}